package org.goochasca

import java.awt.Color

/**
 * Color regions on map. Regions can be either 
 * <a href="http://code.google.com/apis/chart/isocodes.html">countries</a> or 
 * <a href="http://code.google.com/apis/chart/statecodes.html">states in the USA</a>.
 * The regions provided must be present in the map area defined in constructor.
 * @param width Map width. Cannot exceed 440 px.
 * @param height Map height. Cannot exceed 220 px.
 * @param area Map display area
 * @param colorRange Range of colors
 * @param defaultColor The default region color. This is for regions that don't have a level specified
 * @param colorGradient A series of colors that form a gradient
 * @param regionColorLevels Color level per region. Must be within the given range 
 */
final class GeographicMap(
  width: Int, height: Int,
  area: Area,
  defaultColor: Color, 
  colorRange: Range,
  colorGradient: Seq[Color],
  regionColorLevels: Map[String, Int]
) extends Chart(width, height, colorRange, Seq(Data(regionColorLevels.values.toSeq: _*)) ) {
  
  protected val chartType = "t"

  /**
   * @param area Map display area
   * @param defaultColor The default region color. This is for regions that don't have a level specified
   * @param colorRange Range of colors
   * @param colorGradient A series of colors that form a gradient
   * @param regionColorLevels Color level per region. Must be within the given range 
   */
  def this(area: Area, defaultColor: Color, colorRange: Range, 
      colorGradient: Seq[Color], regionColorLevels: Map[String, Int]) =
    this(440, 220, area, defaultColor, colorRange, colorGradient, regionColorLevels)

  /**
   * @param area Map display area
   */
  def this(area: Area, color: Color) = this(440, 220, area, color, 0 to 0, Seq.empty, Map(""->0))

  /**
   * @param width Map width. Cannot exceed 440 px.
   * @param height Map height. Cannot exceed 220 px.
   * @param area Map display area
   */
  def this(width: Int, height: Int, area: Area, color: Color) = this(width, height, area, color, 0 to 0, Seq.empty, Map(""->0))
  
  if (width > 440 || height > 220)
    throw new IllegalArgumentException("Cannot exceed 440x220")
  
  private val colors: Collection[Color] = Some(defaultColor) ++ colorGradient
  
  protected override def appendParms(sb: StringBuilder) {
    super.appendParms(sb)
    sb append "&chtm=" append area.text
    if (!regionColorLevels.isEmpty) {
      Colorific.appendColors(colors, sb)
      sb append "&chld="
      for (region <- regionColorLevels.keys) {
        sb append region
      }
    }
  }
}
